package com.weakteam.mobile.minimum.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.weakteam.mobile.minimum.R;
import com.weakteam.mobile.minimum.common.util.CurrentLocationManager;
import com.weakteam.mobile.minimum.common.util.SimpleLog;
import com.weakteam.mobile.minimum.service.ITaskEvent;
import com.weakteam.mobile.minimum.service.ServerSession;
import com.weakteam.mobile.minimum.service.task.Authenticate;
import com.weakteam.mobile.minimum.service.task.GetCities;
import com.weakteam.mobile.minimum.state.UserSettings;
import com.weakteam.mobile.minimum.ui.widget.CustomFontEditText;
import com.weakteam.mobile.minimum.ui.widget.PhoneNumberEditField;

public class Login extends Activity {

    private PhoneNumberEditField phoneNumberField;
    private CustomFontEditText passwordField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        final CurrentLocationManager manager =  CurrentLocationManager.getInstance().init(this);

        if (manager.getPosition() != null) {
            Log.i("simple", "lon: " + manager.getPosition().getLongitude() + ", lan: " + manager.getPosition().getLatitude());
        }

        this.phoneNumberField = (PhoneNumberEditField)this.findViewById(R.id.phoneNumber);
        this.passwordField = (CustomFontEditText)this.findViewById(R.id.password);

        final Button loginButton = (Button)this.findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Login.this.authenticate();
            }
        });

        final GetCities task = ServerSession.getInstance(this).buildAsyncRequest(GetCities.class);
        task.setOnPostExecute(new ITaskEvent() {
            @Override
            public void onAction() {
                SimpleLog.info(task.getCities().toString());
            }
        });
        task.send();
    }

    @Override
    protected void onResume() {
        super.onResume();
        UserSettings.getInstance().load(this);
        this.phoneNumberField.setPhoneNumber(UserSettings.getInstance().getLogin());
        this.passwordField.setText(UserSettings.getInstance().getPassword());
    }

    private void authenticate() {
        final String phoneNumber = this.phoneNumberField.getPhoneNumber();
        final String password = this.passwordField.getText().toString();

        if (phoneNumber == null || phoneNumber.length() < PhoneNumberEditField.PHONE_NUMBER_LENGTH) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(Login.this);
            alertDialog.setMessage(R.string.activity_login_alert_phone_number);
            alertDialog.setPositiveButton("Ok", null);
            alertDialog.show();
        } else if (password == null || password.length() == 0) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(Login.this);
            alertDialog.setMessage(R.string.activity_login_alert_password);
            alertDialog.setPositiveButton("Ok", null);
            alertDialog.show();
        } else {
            final Authenticate task = ServerSession.getInstance(this).buildAsyncRequest(Authenticate.class);
            task.setPhoneNumber(phoneNumber);
            task.setPassword(password);
            task.setOnPostExecute(new ITaskEvent() {
                @Override
                public void onAction() {
                    if (task.isSuccess()) {
                        UserSettings.getInstance().setLogin(phoneNumber);
                        UserSettings.getInstance().setPassword(password);
                        UserSettings.getInstance().save(Login.this);
                        Intent intent = new Intent(Login.this, Main.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                        startActivity(intent);
                    } else {
                        final String message = task.getErrorMessage();
                        AlertDialog.Builder alertDialog = new AlertDialog.Builder(Login.this);
                        alertDialog.setMessage(message);
                        alertDialog.setPositiveButton("Ok", null);
                        alertDialog.show();
                    }
                }
            });
            task.send();
        }
    }

}
